#include "LogoScene.h"
#include "StartGameScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace cocos2d::extension;


CCScene* LogoScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    LogoScene *layer =  new LogoScene();
	if (layer && layer->init(scene)) {
		layer->autorelease();
	} else {
		delete layer;
		layer = NULL;
	}
    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool LogoScene::init(CCScene* scene)
{
    //////////////////////////////
    // 1. super init first
	do 
    {
		CC_BREAK_IF(! CCLayer::init());
		d_gameUtil = new GameUtil(scene);
		_winSize = CCDirector::sharedDirector()->getWinSize(); 
    
		//CCNode *pNode = CCSSceneReader::sharedSceneReader()->createNodeWithSceneFile("SceneProject.json");
		m_pUiLayer = UILayer::create();
		addChild(m_pUiLayer);
        
		m_pWidget = dynamic_cast<Layout*>(CCUIHELPER->createWidgetFromJsonFile("MyGame_00_1.json"));
		m_pUiLayer->addWidget(m_pWidget);

		// panel
		UIPanel *root = dynamic_cast<UIPanel*>(m_pUiLayer->getWidgetByName("root"));
		root->setPosition(ccp(_winSize.width/2, _winSize.height/2));
		root->setAnchorPoint(ccp(0.5f, 0.5f));

		schedule(SEL_SCHEDULE(&LogoScene::playEffect), 0.0f, 0, 0.0f);
		schedule(SEL_SCHEDULE(&LogoScene::finished), 1.0f, 0, 4.0f);

		// preload background music and effect
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet01.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet02.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet03.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet04.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet05.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet06.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("sound/bullet07.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/logo.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/start.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/main.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/boss.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/upgrade.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/end.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/win.mp3");
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic("sound/gameover.mp3");

		GameUtil::instance()->updateVolume();
	} while (0);
	return true;
}

void LogoScene::playEffect(float dt)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("sound/logo.mp3");
}

void LogoScene::finished(float dt)
{
    CCDirector::sharedDirector()->replaceScene(StartGameScene::scene());
}
